<?php

/**
 * ECSHOP 品牌专区
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: brands.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$b_id = !empty($_GET['b_id']) ? intval($_GET['b_id']) : 0;
$cat_id = !empty($_GET['cat_id']) ? intval($_GET['cat_id']) : 0;
$get_sort  = !empty($_REQUEST['sort']) ? $_GET['sort'] : 'goods_id';
$get_order  = !empty($_REQUEST['order']) ? $_GET['order'] : 'DESC';
if ($b_id > 0)
{
	//对应商品列表
    $order_rule = 'ORDER BY g.goods_id desc, g.sort_order';
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :20;
	
	$cat = get_brand_cat($b_id);
	if ($cat_id == 0)
	{
		$cat_id = $cat[0]['id'];
	}
	$cat_goods = get_brand_goods($b_id, $cat_id, $order_rule, $pageNo, $pageSize);
	foreach ($cat_goods as $key => $goods_data)
	{
		$goods_data['goods_img'] = OSS_HOST.get_image_path($goods_data['goods_id'],$goods_data['goods_img'],false,'goods');
//		$goods_data['goods_img'] = OSS_HOST.get_image_path($goods_data['goods_id'],"");
		$cat_goods[$key] = $goods_data;
	}
	
	echo json_encode(array("cat"=>$cat, "goods"=>$cat_goods));
}
/**
 * 获得指定的品牌下的商品类型
 *
 * @access  public
 * @param   integer     $brand_id       品牌的ID
 * @return  void
 */
function get_brand_cat($brand_id)
{
			
    $sql =  'SELECT c.top_id ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON g.cat_id = c.cat_id ' .
            "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.brand_id = '$brand_id'";
    $sql .= ' ORDER BY c.top_id ASC';
	
	$row = $GLOBALS['db']->getAll($sql);
	$cat_arr = array_column($row, 'top_id');	
	$cat = array();
 	foreach (array_unique($cat_arr) AS $k => $v)
 	{
 		$cat_name = $GLOBALS['db']->getOne("SELECT cat_name FROM ".$GLOBALS['ecs']->table('category'). ' WHERE cat_id = '.$v);
  		array_push($cat, array("id"=>$v, "name"=>$cat_name));
	}

    return $cat;
}

?>